<%--<link rel="stylesheet" media="all" href="/springmvc/static/layui/css/layui.css">
<script src="/springmvc/static/layui/layui.js"></script>
<script src="/springmvc/static/echarts/echarts.js"></script>
<script src="/springmvc/static/js/html2canvas.js"></script>
<script src="/springmvc/static/js/jspdf.debug.js"></script>
<script src="/springmvc/static/js/jq.js"></script>--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common.jsp"%>

<html>
<head>
    <title>SAS测试结果页面</title>
    <script src="${pageContext.request.contextPath}/static/js/echarts.js"></script>
    <style>
        #show {
            /*width: 600px;*/
            /*height: 800px;*/
            /*border: 4px solid cornflowerblue;*/
            margin: auto;

            background: #C2BE9E;
            font-family: 华文行楷;
            font-size: 25px;
            /*绝对定位也行，但是父也不占位。包含块布局的时候一般父相对  父相子绝*/
        }

        #bb {
            width: 200px;
            height: 80px;
            margin: auto;
            line-height: 80px;
        }

        #result {
            width: 400px;
            height: 80px;
            margin: auto;
            line-height: 80px;

        }
    </style>
</head>
<body>
<from style="display: none" id="question-answer">
    欢迎查看<span id="name"></span>的答题
    <table id="demo2" lay-filter="test"></table>
</from>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger " lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>

<script type="text/html" id="rowBarDemo">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="checkQuestion">查看结果</a>
    <a class="layui-btn layui-btn-sm" lay-event="return">回访记录</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="check">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" placeholder="请输入姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">测试计划ID</label>
        <div class="layui-input-inline">
            <select name="testPlanId" lay-filter="type" id="testPlanId">
                <%--                    js填充题目类型下拉选项--%>
            </select>
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">学生类型</label>
        <div class="layui-input-inline">
            <select name="studentType" lay-filter="type" id="questionman">
                <option selected value="1">查询所有</option>
                <option value="2">正常学生</option>
                <option value="3">问题学生</option>
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="selectFormfilter">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="myButton">重置</button>
        </div>
    </div>
</form>

<div id="show" style="width: 100%;height: 100%; display: none;">
    <div id="bb">SAS测试结果</div>
    <div id="result" style="text-align: center"></div>
    <div style="width: 100%;height: 400px;" id="shopping"></div>
</div>

<%-- 添加回访表单  --%>
<form class="layui-form" action="" id="returnRecord"  lay-filter="returnRecord"  style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">测试者id</label>
        <div class="layui-input-inline">
            <input type="text" name="testerId" placeholder="请输入问题序号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">测试者姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="testerName" placeholder="请输入问题"
                   autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="text" name="phone" placeholder="请输入手机号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">计划名称</label>
        <div class="layui-input-inline">
            <input type="text" name="testName" placeholder="请输入参加计划名称"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">回访记录</label>
        <div class="layui-input-inline">
            <textarea name="record"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-inline">
            <input type="text" name="remark" placeholder="请输入备注"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addUserFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>



<table id="demo" lay-filter="test"></table>
</body>
<script>


    layui.config({
    }).use(['table', 'jquery', 'form', 'laydate'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery, laydate = layui.laydate;
        //第一个实例
        var myTable = table.render({
            elem: '#demo'
            , height: 550
            , url: '${pageContext.request.contextPath}/testResultSas/queryAll.do' //数据接口
            , page: true //开启分页
            , limit: 5
            , limits: [5, 10, 15, 20, 25, 30, 50, 100]
            , toolbar: '#barDemo'
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', width: 120, sort: true}
                    , {field: 'name', title: '姓名', width: 120}
                    , {field: 'phone', title: '电话', width: 120}
                    , {field: 'createTime', title: '测试时间', width: 120}
                    , {field: 'forward', title: '正向得分', width: 120, sort: true}
                    , {field: 'inversion', title: '反向得分', width: 120, sort: true}
                    , {field: 'testPlanId', title: '测试计划ID', width: 120, sort: true}
                    , {field: 'testName', title: '测试计划名称', width: 100, sort: true}
                    , {field: 'total', title: '总分', width: 80, sort: true}
                    , {fixed: 'right', title: '操作', width: 220, align: 'center', toolbar: '#rowBarDemo'}
                ]
            ]
        });
        /*
        *
        * 行内工具栏
        * */
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'check') {
                //layer.msg('查看操作');
                //弹出层
                layer.open({
                    title: '焦虑测试结果图示',
                    type: 1,
                    content: $('#show'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    area: ['600px', '600px']
                });
                $.ajax({
                    url: "${pageContext.request.contextPath}/testResultSas/queryResultSasById.do",
                    type: "POST",
                    dataType: "JSON",
                    data: {id: obj.data.id},
                    success: function (ret) {
                        var myChart = echarts.init(document.getElementById('shopping'));
                        var forward = ret.data.forward;
                        var inversion = ret.data.inversion;
                        var total = ret.data.total;
                        var string="";
                        if(total<50){
                            string = "恭喜您！您的测试结果为无焦虑"
                        }else if(51<=total<=60){
                            string = "您的测试结果为轻微至轻度焦虑，请注意休息"
                        }else if(61<=total<=70){
                            string = "您的测试结果为中度焦虑，请安排一场旅行，让自己放松一下"
                        }else if(total>70){
                            string = "您的测试结果为重度焦虑，建议及时就医"
                        }
                        $("#result").html(string)
                        var option = {
                            title: {
                                text: 'SAS焦虑测试',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'item'
                            },
                            legend: {
                                orient: 'vertical',
                                left: 'left'
                            },
                            series: [
                                {
                                    name: 'SAS性格占比',
                                    type: 'pie',
                                    radius: '70%',
                                    data: [
                                        {value: forward, name: '正向题得分', itemStyle: {color: '#c51313'}},
                                        {value: inversion, name: '反向题得分', itemStyle: {color: '#1061ba'}},
                                    ],
                                    emphasis: {
                                        itemStyle: {
                                            shadowBlur: 10,
                                            shadowOffsetX: 0,
                                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                                        }
                                    }
                                }
                            ]
                        };
                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    }
                })
            } else if (obj.event === 'del') {
                layer.confirm('您真的删除行么', function (index) {
                    // 删除用户信息
                    $.ajax({
                        url: '${pageContext.request.contextPath}/testResultSas/deleteResultSasById.do',
                        type: 'POST',
                        dataType: 'JSON',
                        data: {id: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                // 关闭对话框
                                layer.closeAll();
                                // 刷新表格数据
                                table.reload('demo');
                            }
                            layer.msg(data.msg);
                        }
                    });
                });
            }else if(obj.event ==='checkQuestion'){
                $("#username").html(data.name);
                table.render({
                    elem: '#demo2'
                    , height: 550
                    , url: '${pageContext.request.contextPath}/testResultSas/queryQuestionSAS.do?id='+data.id
                    , dataType: 'json'
                    , page: true //开启分页
                    , limit: 10
                    , limits: [5, 10,20,50,100]
                    , autoSort: true//关闭自动排序
                    , cols: [[
                        {checkbox: 'checkbox', fixed: 'left'}//左侧复选框
                        , {field: 'id', title: '问题id', width: 100, sort: true, fixed: 'left'}
                        , {field: 'question', title: '问题', width: 100}
                        , {field: 'result', title: '答案', width: 150, sort: true}
                        , {field: 'dictionary_name', title: '题目类型', width: 150, sort: true}
                    ]]
                });
                layer.open({//弹出修改框
                    title: '修改',
                    type: 1,
                    content: $("#question-answer"),
                    area: ('600px', '600px')
                });

            }else if(obj.event === 'return'){
                /**
                 * 预留回访
                 */
                form.val("returnRecord", {
                    "testerId": data.id
                    , "testName": data.testName
                    , "testerName": data.name
                    , "phone": data.phone
                    , "remark": data.remark
                });
                layer.open({//弹出修改框
                    title: '添加回访记录',
                    type: 1,
                    content: $("#returnRecord"),
                    area: ('400px', '360px')
                });

            }
        });



        form.on('submit(addUserFilter)', function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/testResultSas/insertReturn.do"
                , data: data.field
                , dataType: "json"
                , type: "POST",
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        table.reload('demo', {
                            page: {
                                curr: 1
                                //重新从第 1 页开始
                            }
                        });
                    }
                    layer.msg(data.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm('真的删除选中的行么', function (index) {
                            //obj.del(); //删除对应行（tr）的DOM结构
                            //删除数据库中的数据
                            var idList = [];
                            //遍历数组data，抽取所有的id
                            for (let i = 0; i < data.length; i++) {
                                idList[i] = (data[i].id);
                            }
                            $.ajax({
                                url: "${pageContext.request.contextPath}/testResultSas/deleteResultSasById.do",
                                type: "POST",
                                dataType: "JSON",
                                data: {id: idList.join(",")},
                                success: function (ret) {
                                    if (ret.code == 0) {
                                        //刷新表格数据
                                        myTable.reload({
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                        layer.msg(ret.msg, {icon: 6});
                                    } else {
                                        layer.msg(ret.msg, {icon: 5});
                                    }
                                }
                            })
                            layer.close(index);
                            //向服务端发送删除指令
                        });
                    }
                    break;
            }
        })

        //查询测试计划id
        $.ajax({
            url: "${pageContext.request.contextPath}/testResultSas/getPlanSASId.do",
            type: "post",
            dataType: "json",
            success: function (data) {
                //将dept表中的数据添加到下拉列表中去
                if (data.code == 0) {
                    var htmlStr = "<option value='' >选择测试计划<option>";
                    for (let i = 0; i < data.data.length; i++) {
                        htmlStr += "<option value = " + data.data[i].id + ">" + data.data[i].test_name + "<option>"
                    }
                    $("#testPlanId").html(htmlStr);
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render('select');
                    });
                }
            }
        });
        //查询问题
        form.on('submit(selectFormfilter)', function (data) {
        //data.field当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    name: data.field.name,
                    testPlanId: data.field.testPlanId ,
                    studentType: data.field.studentType
                }
                ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //重置查询条件
        $("#myButton").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    name: "" //第一个查询条件
                    , testPlanId: ""
                    , studentType:""
                },
                page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
        });
    })
</script>
</html>
